package org.dreamwork.jasmine2.restful.client.gui.ui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * Created by IntelliJ IDEA.
 * User: seth.yang
 * Date: 2014/12/9
 * Time: 1:10
 */
public class MappedTabPane extends JTabbedPane implements MouseListener {
    public MappedTabPane () {
        super ();
        addMouseListener (this);
    }

    public MappedTabPane (int tabPlacement) {
        super (tabPlacement);
    }

    public MappedTabPane (int tabPlacement, int tabLayoutPolicy) {
        super (tabPlacement, tabLayoutPolicy);
    }

    @Override
    public void addTab (String title, Component component) {
        addTab (title, new CloseIcon (), component);
    }

    @Override
    public void mouseClicked (MouseEvent e) {
        int index = getUI ().tabForCoordinate (this, e.getX (), e.getY ());
        if (index < 0) return;

        Rectangle rect = ((CloseIcon) getIconAt (index)).getBound ();
        if (rect.contains (e.getX (), e.getY ())) {
            this.removeTabAt (index);
        }
    }

    @Override
    public void mousePressed (MouseEvent e) {

    }

    @Override
    public void mouseReleased (MouseEvent e) {

    }

    @Override
    public void mouseEntered (MouseEvent e) {

    }

    @Override
    public void mouseExited (MouseEvent e) {

    }

    public boolean exists (String name) {
        int count = this.getTabCount ();
        for (int i = 0; i < count; i ++) {
            String title = getTitleAt (i);
            if (name.equals (title)) return true;
        }

        return false;
    }
}